package com.webchat.common.service.messagequeue.producer;


import com.webchat.common.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisMessageQueueSender<T, P> extends AbstractMessageQueueSender<T, P> {

    @Autowired
    private RedisService redisService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 普通队列发送
     *
     * @param queue
     * @param message
     * @return
     */
    @Override
    protected boolean doSend(String queue, T message) {
        String messageValue = super.serializeMessage(message);
        redisService.lleftPush(queue, messageValue);
        return true;
    }

    @Override
    protected boolean broadSend(String channel, T message) {
        String messageValue = super.serializeMessage(message);
        redisTemplate.convertAndSend(channel, messageValue);
        return true;
    }

    @Override
    protected boolean prioritySend(String queue, T message, P priority) {
        String messageValue = super.serializeMessage(message);
        return redisService.zadd(queue, messageValue, Long.valueOf(String.valueOf(priority)), -1L);
    }
}
